package com.crane.oss.web.controller.sports;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.utils.CollectionUtil;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.school.dao.AreaDao;
import com.crane.oss.school.dao.ClassesInfoDao;
import com.crane.oss.school.dao.StudentDao;
import com.crane.oss.school.dao.TeacherDao;
import com.crane.oss.school.model.Area;
import com.crane.oss.school.model.ClassesInfo;
import com.crane.oss.school.model.Student;
import com.crane.oss.school.model.Teacher;
import com.crane.oss.sports.XunFeiIndexApiService;
import com.crane.oss.xunfei.req.QueryIndexCourseStatisticRangeReq;
import com.crane.oss.xunfei.req.QueryIndexCourseStatisticReq;
import com.crane.oss.xunfei.req.XunFeiApiRequest;
import com.crane.oss.sports.bean.response.index.BaseCourseTotalResp;
import com.crane.oss.sports.bean.response.index.BaseTotalResp;
import com.crane.oss.sports.bean.response.index.CourseAvgResp;
import com.crane.oss.sports.bean.response.index.CoursesResp;
import com.crane.oss.sports.bean.resquest.QueryBaseCourseReq;
import com.crane.oss.sports.bean.resquest.QueryCourseStatisticReq;
import com.crane.oss.web.BaseBusinessController;
import com.crane.oss.xunfei.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */

@Api(value = "IndexController", tags = {"智慧体育-首页"})
@RestController
@RequestMapping("/sports/index")
public class IndexController extends BaseBusinessController {


    @Resource
    XunFeiIndexApiService xunFeiIndexApiService;

    @Resource
    protected AreaDao areaDao;

    @Resource
    StudentDao studentDao;

    @Resource
    protected ClassesInfoDao classesInfoDao;

    @Resource
    protected TeacherDao teacherDao;

    @ApiOperation(value = "获取学校基本统计信息", notes = "获取学校基本统计信息")
    @PostMapping("/base")
    public SingleResponse<BaseTotalResp> base() {
        BaseTotalResp baseTotalResp=new BaseTotalResp();
        baseTotalResp.setStudentCount(studentDao.selectCount(new LambdaQueryWrapper<Student>().eq(Student ::getPartyId,getPartyId())));
        baseTotalResp.setCampusCount(areaDao.selectCount(new LambdaQueryWrapper<Area>().eq(Area ::getPartyId,getPartyId())));
        baseTotalResp.setTeacherCount(teacherDao.selectCount(new LambdaQueryWrapper<Teacher>().eq(Teacher ::getPartyId,getPartyId()).eq(Teacher ::getType,1)));
        return SingleResponse.of(baseTotalResp);
    }

    @ApiOperation(value = "课堂健康数据", notes = "课堂健康数据")
    @PostMapping("/courses")
    public MultiResponse<CoursesResp> courses() {
        XunFeiApiRequest request=new XunFeiApiRequest();
        ApiListResponse<IndexHeartRecentResp> response=xunFeiIndexApiService.getIndexHeartRecent(request,getPartyId().toString(),null,getUserId().toString());
        if(null != response.getMsg() && response.getMsg().equals("success")){
            List<CoursesResp> list=BeanUtil.copyList(response.getData(),CoursesResp.class);
            return MultiResponse.ofWithoutTotal(list);
        }
        return MultiResponse.ofWithoutTotal(new ArrayList<>());
    }

    @ApiOperation(value = "体育课基础数据", notes = "体育课基础数据")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryBaseCourseReq")
    @PostMapping("/baseCourse")
    public SingleResponse<BaseCourseTotalResp> baseCourse(@RequestBody QueryBaseCourseReq request) {
        BaseCourseTotalResp resp=new BaseCourseTotalResp();
        QueryIndexCourseStatisticReq req =new QueryIndexCourseStatisticReq();
        if(request.getType().equals("0")){
            req.setTime("cycle");
        }else if(request.getType().equals("1")){
            req.setTime("month");
        }else if(request.getType().equals("2")){
            req.setTime("week");
        }else if(request.getType().equals("3")){
            req.setTime("day");
        }
        ApiSingleResponse<IndexCourseStatisticResp> response=xunFeiIndexApiService.getIndexCourseStatistic(req,getPartyId().toString(), null,getUserId().toString());
        if(null != response.getMsg() && response.getMsg().equals("success")){
            resp.setStudentCount(response.getData().getStudentCount());
            resp.setClassCount(response.getData().getClassCount());
            resp.setCourseCount(response.getData().getCourseCount());
            resp.setAllClass(response.getData().getClassCount());
            resp.setTeacherCount(response.getData().getTeacherCount());
            resp.setCourseTime(response.getData().getCourseTime() == null ? "" : response.getData().getCourseTime());
        }
        return SingleResponse.of(resp);
    }


    @ApiOperation(value = "学段30天课堂分析", notes = "学段30天课堂分析")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryCourseStatisticReq")
    @PostMapping("/courseStatistic")
    public SingleResponse<CourseAvgResp> courseStatistic(@RequestBody QueryCourseStatisticReq request) {
        CourseAvgResp resp=new CourseAvgResp();
        XunFeiIndexApiService xunFeiApiService=new XunFeiIndexApiService();
        QueryIndexCourseStatisticRangeReq req =new QueryIndexCourseStatisticRangeReq();
        if(CollectionUtil.isNotEmpty(request.getClassIds())){
            req.setClassIds(Strings.join(request.getClassIds(),','));
        }else {
            if(request.getSchoolPeriod().equals(1)){
                req.setSectionId("xd0");
                req.setNj(Strings.join(request.getGrades(),','));
            }else if(request.getSchoolPeriod().equals(2)){
                req.setSectionId("xd1");
                List<Integer> temp=request.getGrades().stream().map(s -> s-6).collect(Collectors.toList());
                req.setNj(Strings.join(temp,','));
            }else if(request.getSchoolPeriod().equals(4)){
                req.setSectionId("xd2");
                List<Integer> temp=request.getGrades().stream().map(s -> s-9).collect(Collectors.toList());
                req.setNj(Strings.join(temp,','));
            }
        }
        ApiSingleResponse<IndexCourseStatisticRangeResp> response=xunFeiApiService.getIndexCourseStatisticRange(req,getPartyId().toString(), null,"3");
        if(null!=response  && null != response.getData() && response.getMsg().equals(ApiResponse.SUCCESS) ){
            BeanUtil.copyProperties(response.getData(),resp);
            return SingleResponse.of(resp);
        }
        return SingleResponse.of(resp);
    }

}
